//
//  PayRecordVC.m
//  Gas
//
//  Created by 张伟良 on 17/5/25.
//  Copyright © 2017年 HongHu. All rights reserved.
//

#import "RechargeRecordVC.h"
#import "RechargeRecordCell.h"
#import "NSStringExt.h"

@interface RechargeRecordVC ()<UITableViewDelegate,UITableViewDataSource>
{
    MBProgressHUD *_hud;
}

@property(nonatomic,strong) UITableView *tableView;

// 原始数据
@property(nonatomic,strong) NSMutableArray *resourceArray;

@property(nonatomic,strong) NSMutableArray *dataArray;
@property(nonatomic,assign) NSInteger pageNO;
@property (nonatomic,assign) BOOL isRefresh;


@end

@implementation RechargeRecordVC

- (UITableView *)tableView
{
    if (!_tableView) {
        //列表
        _tableView = [UITableView tableViewWithframe:CGRectMake(0, 0, kScreen_Width, kScreen_Height-kTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = [UIColor clearColor];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"充值记录";
    
    [self.view addSubview:self.tableView];

    /////////////////////////////
//    // 下拉刷新
//    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//
//        [self headerRefresh];
//
//
//    }];
//    // 隐藏时间
//    header.lastUpdatedTimeLabel.hidden = YES;
//    self.tableView.mj_header = header;
    
    // 上拉刷新
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        if (self.dataArray.count > 0) {
            
            [self requestData];
        }
        
    }];
    
    self.pageNO = 1;
    self.resourceArray = [NSMutableArray array];
    [self requestData];
}

//- (void)headerRefresh
//{
//    self.isRefresh = NO;
//    self.pageNO = 1;
//    if (self.dataArray.count > 0) {
//        [self.dataArray removeAllObjects];
//
//    }
//
//    [self requestData];
//
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 请求网络
- (void)requestData
{
    
    if (!self.isRefresh) {
        [SVProgressHUD show];
        
    }
    
    NSMutableDictionary  *paramDic=[[NSMutableDictionary  alloc]initWithCapacity:0];
    if ([SPBluetoothManager shareManager].deviceType1 == 0) {
        [paramDic setValue:[SPBluetoothManager shareManager].cardNo forKey:@"communicateNo"];
        
    }
    else {
        [paramDic setValue:[SPBluetoothManager shareManager].cardNo forKey:@"meterNo"];
        
    }
    
    [paramDic setValue:@(self.pageNO) forKey:@"index"];


    [AFNetworking_RequestData requestMethodPOSTUrl:GetChargeRecord dic:paramDic showHUD:NO response:YES Succed:^(id responseObject) {
        
        self.isRefresh = YES;
        [SVProgressHUD dismiss];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        
        id obj = responseObject[@"ListData"];
        //        self.cardModel = [CardModel yy_modelWithJSON:dic];
        
        if ([obj count] > 0) {
            
            // 添加刷新的数据，再重新分类
            [self.resourceArray addObjectsFromArray:obj];
            
            NSMutableArray *arrM = [NSMutableArray array];
            NSMutableArray *arrM1 = [NSMutableArray array];
            
            // 去掉一样日期
            for (NSDictionary *dic in self.resourceArray) {
                RechageRecordModel *model = [[RechageRecordModel alloc] initWithContentsOfDic:dic];
                if (![arrM containsObject:model.chargeTime]) {
                    
                    [arrM addObject:model.chargeTime];
                    
                    TimeModel *timeModel = [[TimeModel alloc] init];
                    timeModel.timeStr = model.chargeTime;
                    [arrM1 addObject:timeModel];
                }
            }

            // 归类
            for (TimeModel *timeModel in arrM1) {
                
                for (NSDictionary *dic in self.resourceArray) {
                    
                    RechageRecordModel *model = [[RechageRecordModel alloc] initWithContentsOfDic:dic];
                    
                    if ([timeModel.timeStr isEqualToString:model.chargeTime]) {
                        [timeModel.headCellArray addObject:model];
                    }
                }
                
            }
            
            self.dataArray = arrM1;

            self.pageNO++;
        }
        
        else {
            
            // 拿到当前的上拉刷新控件，变为没有更多数据的状态
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        [_tableView reloadData];
        
        
    } failure:^(NSError *error) {
        
        self.isRefresh = YES;
        [SVProgressHUD dismiss];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    TimeModel *timeModel = _dataArray[section];
    return timeModel.headCellArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RechargeRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        
        cell = [[RechargeRecordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    TimeModel *timeModel = _dataArray[indexPath.section];
    cell.model = timeModel.headCellArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    TimeModel *timeModel = _dataArray[section];
    return [NSString isSameMonth:timeModel.timeStr];
}

@end
